package com.fhnew.app.ui.activity.userinfo.fragment.yue;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.fhnew.app.R;
import com.fhnew.app.ui.activity.userinfo.FragmentDataChang;
import com.fhnew.app.ui.activity.userinfo.PageDataChangeControl;

/**
 * 了解余额宝与常见问题
 */
public class HelpFragment extends Fragment implements FragmentDataChang {

    private PageDataChangeControl pageDataChangeControl;
    private String conentTag;
    private String content;

    @Override
    public void setListener (PageDataChangeControl pageDataChangeControl) {
        this.pageDataChangeControl = pageDataChangeControl;
    }

    @Override
    public void dataChanged (Bundle bundle) {
        conentTag = bundle.getString("conentTag");
        content = bundle.getString("content");
        initInfoView();
    }

    private RecyclerView helpList;
    private HelpListAdapter helpListAdapter;

    private TextView t_DetailTitle;
    private WebView mWebView;


    private void initView(View view){
        helpList =view.findViewById(R.id.recyclerView_HelpList);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.height = 200;
                return layoutParams;
            }
        };
        helpList.setLayoutManager(layoutManager);
        helpListAdapter = new HelpListAdapter();
        helpList.setAdapter(helpListAdapter);

        t_DetailTitle = view.findViewById(R.id.textVIew_YuE_Help_Title);
        mWebView = view.findViewById(R.id.textVIew_YuE_Help_Content);

    }

    private void initInfoView(){
        if(t_DetailTitle == null || conentTag== null )return;
        if(conentTag.equals("1")){
            t_DetailTitle.setText("余额宝");
        }else{
            t_DetailTitle.setText("常见问题");
        }

        String mimeType = "text/html";
        String enCoding = "utf-8";

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDefaultTextEncodingName("utf-8") ;
        mWebView.loadDataWithBaseURL(null, content, mimeType, enCoding, null);

    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.fragment_yue_help,null);
        initView(container);
        return container;
    }

    @Override
    public void onResume () {
        super.onResume();
        initInfoView();
    }

    private class HelpListAdapter extends RecyclerView.Adapter<HelpListAdapter.ViewHolder>{


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder (@NonNull ViewGroup viewGroup, int i) {
            viewGroup = (ViewGroup) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_help_list,null);
            return new ViewHolder(viewGroup);
        }

        @Override
        public void onBindViewHolder (@NonNull ViewHolder viewHolder, int i) {

        }

        @Override
        public int getItemCount () {
            return 5;
        }

        public class ViewHolder extends RecyclerView.ViewHolder{

            public ViewHolder (@NonNull View itemView) {
                super(itemView);
            }
        }
    }


}
